﻿Imports DTO
Imports System.Data.SqlClient

Public Class CBoPhanDAO
    Public Function LoadDanhSachBoPhan() As List(Of CBoPhanDTO)
        Using cn As SqlConnection = CDataProvider.Connect()
            Dim cm As SqlCommand = New SqlCommand("uspLayDanhSachBoPhan", cn)
            cm.CommandType = CommandType.StoredProcedure
            Dim listBoPhan As New List(Of CBoPhanDTO)()
            Using reader As IDataReader = cm.ExecuteReader(CommandBehavior.CloseConnection)
                While reader.Read()
                    listBoPhan.Add(New CBoPhanDTO(reader))
                End While
            End Using
            Return listBoPhan
        End Using
    End Function


    Public Function ThemBoPhan(ByVal tenbophan As String) As Integer
        Using cn As SqlConnection = CDataProvider.Connect()
            Dim cm As New SqlCommand("usp_ThemBoPhan", cn)
            cm.CommandType = CommandType.StoredProcedure
            Dim para As New SqlParameter("@tenbophan", tenbophan)
            cm.Parameters.Add(para)
            Dim parakq As New SqlParameter("@kt", 0)
            parakq.Direction = ParameterDirection.Output
            cm.Parameters.Add(parakq)
            cm.ExecuteNonQuery()
            Dim kq As Integer = Integer.Parse(cm.Parameters("@kt").Value.ToString())
            cn.Close()
            Return kq
        End Using
    End Function


    Public Function CapNhatBoPhan(ByVal bophan As CBoPhanDTO) As Integer
        Using cn As SqlConnection = CDataProvider.Connect()
            Dim cm As New SqlCommand("usp_CapNhatBoPhan", cn)
            cm.CommandType = CommandType.StoredProcedure
            Dim para As New SqlParameter("@mabophan", bophan.Ma)
            cm.Parameters.Add(para)
            para = New SqlParameter("@tenbophan", bophan.Ten)
            cm.Parameters.Add(para)
            Dim parakt As New SqlParameter("@kt", 0)
            parakt.Direction = ParameterDirection.Output
            cm.Parameters.Add(parakt)

            cm.ExecuteNonQuery()
            Dim kq As Integer = Integer.Parse(cm.Parameters("@kt").Value.ToString())
            cn.Close()
            Return kq
        End Using
    End Function

    Public Function LayTenBoPhanTheoMa(ByVal ma As Integer) As String
        Using cn As SqlConnection = CDataProvider.Connect()
            Dim cm As New SqlCommand("uspLayTenBoPhanTheoMa", cn)
            cm.CommandType = CommandType.StoredProcedure
            Dim para As New SqlParameter("@ma", ma)
            cm.Parameters.Add(para)
            Dim str As String = cm.ExecuteScalar()
            cn.Close()
            Return str
        End Using
    End Function



End Class
